
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  processquery;


import java.util.Hashtable;
import java.util.Vector;

import User.Uservalidation;

import control.RequestParameter;

import pool.bean.Bean;
import processquery.Mod;
import processquery.Query;



import utility.CapitalChar;
import utility.ConvertToLower;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Mod implements Query
{

	String Object;
	GetIdName gid;
	String FIELD="field";
	String NEWVALUE="newvalue"; 
	String OLDVALUE="oldvalue";
	String USERNAME="+911234567891";
	String id="";
	String MASTERNAME="mastername";
	
	public void setObject(String Object) 
	{
		this.Object = Object;
	}
	
	public void setdatbase() 
	{
		gid = new GetIdName();
		FIELD = gid.getId(FIELD);
		NEWVALUE=gid.getId(NEWVALUE);
		OLDVALUE=gid.getId(OLDVALUE);
	}
	
	public void processQuery() 
	{	
		RecordSearch rs =new RecordSearch();
		Hashtable<Object, Object> Condition =new Hashtable<Object, Object>();
		Condition.put("mid", Object);
		rs.setConditionMap(Condition);
		String line[] = rs.getArrayFromResultSet();
		Condition.clear();
		Hashtable<Object, Object> table =new Hashtable<Object, Object>();
		for(int i=0;i<line.length;i++)
		{
			String split[] = line[i].split("\t");
			String value = split[Column.pv_index-1];
			if(value.equals("0"))
				value = split[Column.vt_index-1];
			
			table.put(gid.getItem(split[Column.pid_index-1]), value);
		}
		table = ConvertToLower.convertHashKey(table);
		Vector<Object> Newvalue = new Vector<Object>();
		
		
		
		
	//	String oldquery= table.get(OLDVALUE).toString();
		String oldquery= table.get(gid.getItem(OLDVALUE)).toString();
		query.query.Query query =new query.query.Query();		
		query.initPool();
		query.settypeofreport("Systemgenerate");
		query.setObject(oldquery);
		query.setValue(id);
		query.initializeData();
		query.updateReport();
		String oldbuffer[]=query.getbuffer();
		String oldheading[]  = oldbuffer[0].split("\t");
		Hashtable<Object, Object> OldMasterMap = new Hashtable<Object, Object>();
		Uservalidation user =new Uservalidation();
		String password = user.getPasword(USERNAME);
		
		
		for(int j=1;j<oldbuffer.length;j++)
		{
			String oldsplitbuffer[]= oldbuffer[j].split("\t");
			for(int m=0;m<oldsplitbuffer.length;m++)
			{	
					OldMasterMap.put(oldheading[m], oldsplitbuffer[m]);
			}
		}	
		
		String newquery= table.get(gid.getItem(NEWVALUE)).toString();
		query.initPool();
		query.setObject(newquery);
		query.setValue(id);
		query.initializeData();
		query.updateReport();		
		String buffer[]=query.getbuffer();
		String heading[]  = buffer[0].split("\t");
		Hashtable<Object, Object> MasterMap = new Hashtable<Object, Object>();	
		for(int i=1;i<buffer.length;i++)
		{
			String splitbuffer[]= buffer[i].split("\t");
			for(int k=0;k<splitbuffer.length;k++)
			{	
					MasterMap.put(heading[k], splitbuffer[k]);
			}
			String request = table.get("tablename").toString();
			request = gid.getItem(request);
			if(request.equals("no$"))
				request = table.get("tablename").toString();
			try {
				Class C = Class.forName("pool.bean."+CapitalChar.makeFirstCharCapital(request)+"_Bean") ;
				Bean beanObject=(Bean)C.newInstance();
				beanObject.initPool();
				RequestParameter req = new RequestParameter();
				req.setProcess("add");
				req.setRequest(request);
				req.setUsername(USERNAME);
				req.setPassword(password);
				beanObject.setReqParam(req);
				beanObject.setNewHashtable(ConvertmaptoMastermap(MasterMap));
				beanObject.setOldHashtable(ConvertmaptoMastermap(OldMasterMap));
				beanObject.update();
			} catch (ClassNotFoundException e)
			{
				e.printStackTrace();
			}
			catch (InstantiationException e) 
			{	
				e.printStackTrace();
			}
			catch (IllegalAccessException e) 
			{
				e.printStackTrace();
			}
		}
		
	}// fn.
	
	
	public void Init(String object)
	{
		setdatbase();
		setid("10");
		setObject(object);
		processQuery();
		
	}
	
	public static void main(String[] args) 
	{
		Mod mod=new Mod();
		/*
		mod.setdatbase(database);
		mod.setid("10");
		mod.setObject("2600037");
		mod.processQuery();*/
		mod.Init("2600037");
		
	}
	public Hashtable<Object, Object> ConvertmaptoMastermap(Hashtable<Object, Object> table)
	{
		if(table.containsKey(MASTERNAME))
		{
			Hashtable<Object , Object> result = new Hashtable<Object, Object>();
			result.put("name", table.get(MASTERNAME));
			table.remove(MASTERNAME);
			result.putAll(table);
			return result;
		}
		else
			return table;
	}

	public void setid(String id) 
	{
		this.id =  id;
	}
	
}//end of class Mod
